<?php require_once('../connections/con_gl.php'); ?>
<?php
$act = $_GET[act];
$url = "index.php?component=kinvoice";
if (trim($_POST['kontrak']) == '') {
	$error[] = '- kontrak harus dipilih !!!';
}
if (isset($error)) {
	echo "<img src=\"images/alert.png\" width=\"16\" align=\"left\"/>&nbsp;&nbsp;<b style=\"color:red;\">Error : </b> <br />".implode("<br />", $error);
}else{
	// -- insert rtrans
	$query_insert_1 = "insert into gl_rtrans (no_ref,tgl,keterangan,kontak,jenis,gudang,area,jatuhtempo,akbiaya,akpjk,kontrak) values ('$_POST[noreferensi]','$_POST[tanggal]','$_POST[keterangan]','$_POST[dari]','$_POST[jenis]','$_POST[gudang]','$_POST[area]','$_POST[jatuhtempo]','$_POST[akunbiaya]','$_POST[akunpajak]','$_POST[kontrak]')";
	// hitung kendaraan
	$ken  = $_POST['kendaraan'];
	$sat  = $_POST['satuan'];
	$jum  = $_POST['jumlah'];
	$har  = $_POST['hargasatuan'];
	$dis  = $_POST['diskon'];
	$tot  = $_POST['total'];
	$tra  = $_POST['transaksi'];
	$akn  = $_POST['akun_jual'];
	$bar  = $_POST['barang']; echo $bar;
	$jken = count($ken);
	//--- gen query tran
	$query_insert_2 = "insert into gl_trans(id,no_ref,transaksi,total,pos,akun,barang,kendaraan,jumlah,satuan,hargasatuan,diskon) values ";
	for($i=0;$i<$jken;$i++) {
		$query_insert_2 .= "(NULL,'$_POST[noreferensi]','$tra[$i]','$tot[$i]','K','$akn[$i]','$bar[$i]','$ken[$i]','$jum[$i]','$sat[$i]','$har[$i]','$dis[$i]')";
		if($i < $jken-1) {
			$query_insert_2 .=",";	
		}else{
			$query_insert_2 .=";";
		}
	}
	// save data
	mysql_select_db($database_con_gl, $con_gl);
	$insert_1 = mysql_query($query_insert_1, $con_gl) or die(mysql_error());
	if(insert_1) {
		//--- save transaksi
		mysql_select_db($database_con_gl, $con_gl);
		$insert_2 = mysql_query($query_insert_2, $con_gl) or die(mysql_error());
		// --
		mysql_select_db($database_con_gl, $con_gl);
		$query_getkpd = "SELECT gl_kontak.nama FROM gl_kontak WHERE gl_kontak.id='$_POST[dari]'";
		$getkpd = mysql_query($query_getkpd, $con_gl) or die(mysql_error());
		$row_getkpd = mysql_fetch_assoc($getkpd);
		// ---
		if($_POST[akunkredit] <>'' AND $_POST[ntotal] > 0) {
			mysql_select_db($database_con_gl, $con_gl);
			mysql_query("INSERT INTO gl_trans (id, no_ref, transaksi, total, pos, akun) VALUES (NULL, '$_POST[noreferensi]', 'Piutang Penjualan $row_getkpd[nama]', '$_POST[ntotal]', 'D', '$_POST[akunkredit]')", $con_gl);
		}
		if($_POST[akunbiaya] <>'' AND $_POST[biaya] > 0) {
			mysql_select_db($database_con_gl, $con_gl);
			mysql_query("INSERT INTO gl_trans (id, no_ref, transaksi, total, pos, akun) VALUES (NULL, '$_POST[noreferensi]', 'Beban Biaya Lain', '$_POST[biaya]', 'K', '$_POST[akunbiaya]')", $con_gl) or die(mysql_error());
		}
		if($_POST[akunpajak] <>'' AND $_POST[pajak] > 0) {
			mysql_select_db($database_con_gl, $con_gl);
			mysql_query("INSERT INTO gl_trans (id, no_ref, transaksi, total, pos, akun) VALUES (NULL, '$_POST[noreferensi]', 'Beban Pajak', '$_POST[pajak]', 'K', '$_POST[akunpajak]')", $con_gl) or die(mysql_error());
		}
		echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Generated Invoice berhasil ...";
	}else{
		echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Generated Invoice gagal !!!";	
	}
	// ---
}
?>
<?php if(insert_1) { ?><script type="text/javascript">setTimeout("location.href='<?php echo $url;?>'", 2000);</script><?php } ?>